package com.solotiger.okex.exception;

import com.solotiger.okex.common.result.Result;
import com.solotiger.okex.common.result.ResultCode;
import com.solotiger.okex.common.result.ResultFactory;
import org.apache.shiro.authz.AuthorizationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 */
@RestControllerAdvice
public class MyExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(MyException.class)
    public Result handleMyException(MyException e) {
        System.out.println("handleMyException");
        return ResultFactory.INSTANCE.error(e.getMsg());
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuthorizationException(AuthorizationException e){
        logger.error(e.getMessage(), e);
        System.out.println("handleAuthorizationException");
        return ResultFactory.INSTANCE.result(ResultCode.UNAUTHORIZED,"没有权限，请联系管理员授权");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return ResultFactory.INSTANCE.result(ResultCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
